package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yaoxinjia
 */
@Controller
public class SeckillController {


    @Autowired
    private SeckillService seckillService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    private final String TEST_KEY = "test:key";



    @GetMapping(value = "redis/test/decrease")
    @ResponseBody
    public R redisStockDecrease(){
        String s = redisTemplate.opsForList().leftPop(TEST_KEY);
        if(s!=null){
            System.out.println("秒杀成功");
        }else{
            System.out.println("秒杀失败");
        }

        return R.ok().put("dec",s);
    }

    @GetMapping(value = "redis/test/increase")
    @ResponseBody
    public R testRedisLeftPush(){
        Long aLong = redisTemplate.opsForList().leftPush(TEST_KEY, "1");


        return R.ok().put("inc",aLong);
    }

    /**
     * 当前时间可以参与秒杀的商品信息
     * @return
     */
    @GetMapping(value = "/getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus() {

        //获取到当前可以参加秒杀商品的信息
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();

        return R.ok().setData(vos);
    }


    /**
     * 根据skuId查询商品是否参加秒杀活动
     * @param skuId
     * @return
     */
    @GetMapping(value = "/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckilInfo(@PathVariable("skuId") Long skuId) {

        SeckillSkuRedisTo to = seckillService.getSkuSeckilInfo(skuId);

        return R.ok().setData(to);
    }


    /**
     * 商品进行秒杀(秒杀开始)
     * @param killId
     * @param key
     * @param num
     * @return
     */
    @GetMapping(value = "/kill")
    @ResponseBody
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        String orderSn = null;
        try {
            //1、判断是否登录
            orderSn = seckillService.kill(killId,key,num);
            model.addAttribute("orderSn",orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    //信号量作为分布式信号量限流
    @RequestMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");//获取一个信号量
//        park.acquire();//获取一个信号，获取一个值，占一个坑 如果获取不到就一直等待阻塞
        park.tryAcquire();//这个不会阻塞等待 获取就返回true否则false
        return "ok";
    }

    @RequestMapping("/go")
    @ResponseBody
    public String go(){
        RSemaphore park = redissonClient.getSemaphore("park");//key 要相同
        park.release();//释放一个信号
        return "ok";
    }

}
